package cn.xorange.commons.core.auth.service;

import cn.xorange.commons.core.auth.mapper.UserPermissionMapper;
import cn.xorange.commons.security.configuration.SecurityProperties;
import cn.xorange.commons.security.constants.SecurityConstants;
import cn.xorange.commons.security.entity.*;
import cn.xorange.commons.security.exception.SecurityException;
import cn.xorange.commons.security.mapper.LoginTenantMapper;
import cn.xorange.commons.core.constants.SysConstants;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author : yangjian
 * @date : 2023/12/20
 */
public class UserPermissionService {

    @Resource
    SecurityProperties securityProperties;

    @Autowired
    UserPermissionMapper mapper;

    @Resource
    LoginTenantMapper tenantMapper;


    public void setTenantOwnerAdmin(SecurityUser user){
        //开启多租户模式绑定租户信息
        if(securityProperties.isEnableTenant()){
            if (Objects.isNull(user.getTenantId())){
                throw SecurityException.systemError("参数错误,租户Id为空");
            }
            SecurityTenant tenant = this.tenantMapper.selectTenantById(user.getTenantId());;
            if(tenant.getPhone().equals(user.getPhone())){
                user.setIsAdmin(SysConstants.FLAG_YES);
            }
            user.setTenantName(tenant.getTenantName());
        }
    }

    public void setUserRoles(SecurityUser user){
        if(user.getIsAdmin()){
            user.setRoles(new ArrayList<>());
        }else {
            List<SecurityRole> roleList = this.mapper.selectRoleByUserId(user.getTenantId(), user.getUserId());
            for(SecurityRole role : roleList){
                if(SecurityConstants.ADMIN_PERMISSION.equals(role.getRoleKey())){
                    user.setRoles(new ArrayList<>());
                    user.setIsAdmin(SysConstants.FLAG_YES);
                }
            }

            if(!user.getIsAdmin()){
                List<SecurityRolePermission> rolePermissions = this.mapper.selectRolePermsByUserId(user.getTenantId(), user.getUserId());
                List<SecurityRolePermission> rPKeys = new ArrayList<>();

                for(SecurityRole r : roleList){
                    SecurityRolePermission srp = new SecurityRolePermission();
                    srp.setRoleId(r.getRoleId());
                    for (SecurityRolePermission p : rolePermissions){
                        if(r.getRoleId().equals(p.getRoleId())){
                            srp.setPermissionKey(p.getPermission());
                        }
                    }
                    rPKeys.add(srp);
                }
                List<SecurityRole> lst = new ArrayList<>();
                for(SecurityRole r : roleList){
                    for(SecurityRolePermission srp : rPKeys){
                        if(r.getRoleId().equals(srp.getRoleId())){
                            r.setPermissions(srp.getPermissionKeys());
                            lst.add(r);
                        }
                    }

                }
                user.setRoles(lst);
            }
        }
    }


    public void setUserRoleKeys(SecurityUser user){
        HashSet<String> roles = new HashSet<>();
        if(user.getIsAdmin()){
            roles.add(SecurityConstants.ADMIN_ROLE);
        } else {

            for(SecurityRole r : user.getRoles()){
                roles.add(r.getRoleKey());
            }
        }
        user.setRoleKeys(roles);
    }

    public void setUserPermissionKeys(SecurityUser user){
        HashSet<String> set = new HashSet<>();
        if (user.getIsAdmin()){
            set.add(SecurityConstants.ADMIN_PERMISSION);
        }else {
            for(SecurityRole r : user.getRoles()){
                for(String per : r.getPermissions()){
                    if(Objects.nonNull(per) && !per.isEmpty()){
                        set.add(per);
                    }
                }
            }
        }
        user.setPermissions(set);
    }





    //public void setDataScope(SecurityUser sysUser){
    //
    //    ArrayList<RoleOrg> rList = new ArrayList<>();
    //    ArrayList<RoleOrg> rmList = new ArrayList<>();
    //    if(Objects.isNull(sysUser.getDataScope())){
    //        if(GlobalConstants.FLAG_YES.equals(sysUser.getIsAdmin())){
    //            RoleOrg r = new RoleOrg();
    //            r.setDataScope(DataConstants.USER_DATA_SCOPE_ALL);
    //            rList.add(r);
    //            RoleOrg rm = new RoleOrg();
    //            rm.setDataScope(DataConstants.USER_DATA_SCOPE_ALL);
    //            rmList.add(rm);
    //        }else {
    //            List<RoleOrg> list = mapper.selectRoleOrg(sysUser.getTenantId(),sysUser.getUserId());
    //            rmList = this.setRoleOrgList(list, DataConstants.ROLE_TYPE_MANAGE,sysUser.getOrgId(),sysUser.getOrgNode());
    //            rList = this.setRoleOrgList(list, DataConstants.ROLE_TYPE_NORMAL,sysUser.getOrgId(),sysUser.getOrgNode());
    //        }
    //        sysUser.setDataScope(rList);
    //    }
    //}

    //private ArrayList<RoleOrg> setRoleOrgList(List<RoleOrg> list, String type, Long orgId, String orgLink ) {
    //
    //    /** 本部门及以下的数据 */
    //    RoleOrg o2 = new RoleOrg();
    //    /** 本部门 */
    //    RoleOrg o5 = new RoleOrg();
    //    /** 自己 */
    //    RoleOrg o6 = new RoleOrg();
    //
    //    /** 自定义部门及以下的数据 */
    //    ArrayList<RoleOrg> lst3 = new ArrayList<>();
    //
    //    for(RoleOrg r : list) {
    //        if(type.equals(r.getRoleType())) {
    //            if(DataConstants.USER_DATA_SCOPE_ORG_LINK.equals(r.getDataScope())){
    //                /** 2本部门及以下的数据 */
    //                o2.setDataScope(DataConstants.USER_DATA_SCOPE_ORG_LINK);
    //                o2.setOrgId(orgId);
    //                o2.setTreeNode(orgLink);
    //            } else if(DataConstants.USER_DATA_SCOPE_CUSTOM_ORG_LINK.equals(r.getDataScope())){
    //                /** 3自定义部门及以下的数据 */
    //                lst3.add(r);
    //            } else if(DataConstants.USER_DATA_SCOPE_CUSTOM_ORG.equals(r.getDataScope())){
    //                /** 4用户自定义的部门数据 */
    //                lst3.add(r);
    //            } else if(DataConstants.USER_DATA_SCOPE_ONLY_OUR_ORG.equals(r.getDataScope())){
    //                /** 5本部门 */
    //                o5.setDataScope(DataConstants.USER_DATA_SCOPE_ONLY_OUR_ORG);
    //                o5.setOrgId(orgId);
    //            }else if(DataConstants.USER_DATA_SCOPE_ONLY_OUR.equals(r.getDataScope())){
    //                /** 6自己 */
    //                o6.setDataScope(DataConstants.USER_DATA_SCOPE_ONLY_OUR);
    //            }
    //
    //        }
    //    }
    //
    //    /** 6自己 */
    //    if(lst3.isEmpty() && Objects.nonNull(o6.getOrgId())){
    //        lst3.add(o6);
    //        return lst3;
    //    }
    //
    //
    //    boolean o2IsHave = false;
    //    boolean o5IsHave = false;
    //    /** 不为空的情况下 lst3 只包含 自定义部门及一下，和 自定义部门的数据 */
    //    for(RoleOrg r : lst3){
    //        /** 3自定义部门及以下的数据 */
    //        if(DataConstants.USER_DATA_SCOPE_CUSTOM_ORG_LINK.equals(r.getDataScope())){
    //            if(Objects.nonNull(o2.getOrgId()) && r.getOrgId().equals(o2.getOrgId())){
    //                o2IsHave = true;
    //            }
    //        }else {
    //            /** 4用户自定义的部门数据 */
    //            if(Objects.nonNull(o5.getOrgId()) && r.getOrgId().equals(o5.getOrgId())){
    //                o5IsHave = true;
    //            }
    //        }
    //    }
    //
    //    if(Boolean.FALSE.equals(o2IsHave)){
    //        lst3.add(o2);
    //    }
    //    if(Boolean.FALSE.equals(o5IsHave)){
    //        lst3.add(o5);
    //    }
    //    return lst3;
    //}
}
